package nciae.db.controller;

import nciae.db.common.CommonResult;
import nciae.db.pojo.entity.Expert;
import nciae.db.pojo.vo.QueryExportVO;
import nciae.db.service.ExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class ExpertController {
    @Autowired
    private ExpertService expertService;

    @ResponseBody
    @GetMapping("/expert/listExpertName")
    public CommonResult<List<String>> listExpertName() {
        return expertService.listExpertName();
    }

    @ResponseBody
    @RequestMapping("/expert/assignList")
    public CommonResult<List<QueryExportVO>> list(){
        return expertService.getAllExperts();
    }

    @GetMapping("/expert/index")
    public String index(){
        return "expert/index";
    }

    @ResponseBody
    @RequestMapping("/expert/list")
    public CommonResult<List<Expert>> list(Integer page, Integer limit, String name, String department, String title){
        return expertService.search(page, limit, name, department, title);
    }

    @GetMapping("/expert/form")
    public ModelAndView form(Integer id, Model model){
        ModelAndView mv = new ModelAndView();
        Expert expert;
        if (id == 0){
             expert = new Expert();
            expert.setId(0);
        }
        else {
             expert = expertService.getExpertInfo(id);
        }
        mv.setViewName("expert/form");
        model.addAttribute("model",expert);
        return mv;
    }

    @ResponseBody
    @RequestMapping("/expert/save")
    public CommonResult save(@RequestBody Expert expert){
            CommonResult<Expert> result = expertService.expertSave(expert);
            return result;
    }

    @ResponseBody
    @RequestMapping("/expert/del")
    public CommonResult<Boolean> del(Integer id){
        return expertService.delete(id);
    }
}
